package generator.service.impl;

import com.atkt.ktkt.model.vod.Video;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import generator.service.VideoService;
import generator.mapper.VideoMapper;
import generator.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 罗雅文
 * @description 针对表【video(课程视频)】的数据库操作Service实现
 * @createDate 2022-09-18 17:25:21
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
        implements VideoService {

    @Autowired
    private VodService vodService;

    //根据课程id 删除小节
    @Override
    public void removeVideoByCourseId(Long id) {
        //1.根据课程id 查询课程所有小节
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Video::getCourseId, id);
        List<Video> videos = baseMapper.selectList(wrapper);
        // 遍历所有小节集合得到每个小节 获取每个小节视频id
        videos.forEach(item->{
            String videoSourceId = item.getVideoSourceId();
            //判断视频id是否为空 不为空 删除腾讯云视频
            if (!StringUtils.isEmpty(videoSourceId)){
                vodService.removeVideo(videoSourceId);
            }
        });
        //根据课程id 删除课程所有小节
       baseMapper.delete(wrapper);
    }

    //删除小节 同时删除小节里面视频
    @Override
    public void removeVideoById(Long id) {
        //id查询小节信息
        Video video = baseMapper.selectById(id);
        //获取 video 视频id
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            //视频id不为空 调用方法根据视频id 删除腾讯云视频
            vodService.removeVideo(videoSourceId);
        }
        //根据id删除小节
        baseMapper.deleteById(id);

    }
}




